package se.webbzon.oschi01.gui;

import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.ListModel;

public class ModelUtils {
	
	public static final <T> ListModel<T> toListModel(Collection<T> c) {
		SynchronizedListModel<T> model = new SynchronizedListModel<T>();
		for (T t : c) {
			model.addElement(t);
		}
		return model;
	}
	
	public static final <K,V> ListModel<Entry<K,V>> toListModel(Map<K,V> map) {
		SynchronizedListModel<Entry<K,V>> model = new SynchronizedListModel<Entry<K,V>>();
		for (Entry<K,V> entry : map.entrySet()) {
			model.addElement(entry);
		}
		return model;
	}
	
	public static final <T> SortedListModel<T> toSortedListModel(ListModel<T> model, Comparator<T> cmp) {
		return new SortedListModel<T>(model,cmp);
	}
	
	public static final <T> SortedListModel<T> toSortedListModel(Collection<T> c, Comparator<T> cmp) {
		SortedListModel<T> model = new SortedListModel<T>(cmp);
		model.addAll(c);
		return model;
	}
	
	public static final <K,V> SortedListModel<Entry<K,V>> toSortedListModel(Map<K,V> c, Comparator<Entry<K,V>> cmp) {
		SortedListModel<Entry<K,V>> model = new SortedListModel<Entry<K,V>>(cmp);
		model.addAll(c.entrySet());
		return model;
	}

}
